package com.neutech.frame;

import com.neutech.Util.ImageUtils;
import com.neutech.base.*;
import com.neutech.constant.Constant;
import com.neutech.img.tank.Home;
import com.neutech.terrain.*;
import com.neutech.tank.EnemyTank;
import com.neutech.tank.Tank;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.util.Map;

/**
 * 坦克的窗口类
 */
public class TankFrame extends Frame {

    public TankFrame(){

        init();
    }
    private EnemyTank enemyTank;


    /**
     *私有的初始化方法，再让上面的调用，因为不让在构造函数里面写逻辑
     */
    private void init(){
        /**
         * 写逻辑，多窗口的基本设置
         */
        /**
         * 调用地图,放到最前面，最先执行
         */
        loadMap();
        setSize(Constant.FRAME_WIDTH,Constant.FRAME_HEIGHT);
        /**
         * 设置居中
         */
        setLocationRelativeTo(null);
        /**
         * 设置窗口不可改变
         */
        setResizable(false);
        /**
         *
         * 设置关闭输入法，打开游戏就不会再显示输入法了
         */

        enableInputMethods(false);

        /**
         * 设置显示
         */

        /**
         * 设置窗口图片文字
         */
        setTitle("坦克大战终极版");

        setIconImage(ImageUtils.getValue("tank_y_1_1"));
        setVisible(true);


        /**
         * 处理图片不显示，刷新窗口就行
         * 刷新之后发现任然没有坦克，结果是因为函数没有调用，去tank里面调用
         */
        new Thread(new Runnable() {
            @Override
            public void run() {
          while (true){
              repaint();
              try {
                  Thread.sleep(10);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }

          }
            }
        }).start();
        /**
         * 添加一个窗口监听事件
         */
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {

                System.exit(0);
            }
        });
        addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {
                if (gameStatus == 1){
                 tankSeletor.keyPressed(e);

                }else if (gameStatus == 2){
                    tankMap.p1.keyPressed(e);
                }else if (gameStatus == 3){
                    if (tankMap.p1 !=null){tankMap.p1.keyPressed(e);}
                    if (tankMap.p2!=null){ tankMap.p2.keyPressed2(e);}
                }else if (gameStatus == 4){
                    mapGenerator.keyPressed(e);
                }
                else if (gameStatus == 5){
                    tankSummary.keyPressed(e);

                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
                if (gameStatus == 1){

                }else if (gameStatus == 2){
                    tankMap.p1.keyReleased(e);
                }else if (gameStatus == 3){
                    if (tankMap.p1!=null){tankMap.p1.keyReleased(e);}
                    if (tankMap.p2!=null){tankMap.p2.keyReleased2(e);}

                }

            }
        });

    }

//    Tank tank = new Tank();
    /**
     * 坦克地图
     */
    TankMap tankMap;
    TankSeletor tankSeletor = new TankSeletor(this);
    MapGenerator mapGenerator;
    TankSummary tankSummary = new TankSummary(this);
    //Prop prop;

//    Home home = new Home(this);

//状态码  让左面调用方法去改变值
    public void changeStatus(int gameStatus){
        this.gameStatus = gameStatus;
        if (this.gameStatus == 2){
            loadMap();
            tankMap.p1 = new Tank(Constant.MARGIN_LEFT+Constant.SPRITE_SIZE*4,
                    Constant.MARGIN_TOP + Constant.SPRITE_SIZE*12,
                    Constant.SPRITE_SIZE,Constant.SPRITE_SIZE);
        }
        if (this.gameStatus == 3){
            loadMap();
            tankMap.p1 = new Tank(Constant.MARGIN_LEFT+Constant.SPRITE_SIZE*4,
                    Constant.MARGIN_TOP + Constant.SPRITE_SIZE*12,
                    Constant.SPRITE_SIZE,Constant.SPRITE_SIZE);

            tankMap.p2 = new Tank(Constant.MARGIN_LEFT+Constant.SPRITE_SIZE*8,
                    Constant.MARGIN_TOP + Constant.SPRITE_SIZE*12,
                    Constant.SPRITE_SIZE,Constant.SPRITE_SIZE);

        }
    if ( this.gameStatus  == 4){
        //不保留数据的，改变到这个状态new一个新的
        mapGenerator = new MapGenerator(this);
    }
    if (this.gameStatus == 5){
        tankSummary = new TankSummary(this);
    }

    }

    /**
     * 家在地图的
     */
    public void loadMap(){
        InputStream is = null;
        ObjectInputStream ois = null;
        try {
            is = new FileInputStream("src/com/neutech/map/stage1.tankmap");
           ois = new ObjectInputStream(is);
           tankMap = (TankMap) ois.readObject();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        tankMap.enemyTankList.add(new EnemyTank(Constant.MARGIN_LEFT + Constant.SPRITE_SIZE,Constant.MARGIN_TOP,Constant.SPRITE_SIZE,Constant.SPRITE_SIZE));
        tankMap.enemyTankList.add(new EnemyTank(Constant.MARGIN_LEFT+Constant.SPRITE_SIZE*4,Constant.MARGIN_TOP ,Constant.SPRITE_SIZE,Constant.SPRITE_SIZE));
        tankMap.enemyTankList.add(new EnemyTank(Constant.MARGIN_LEFT+Constant.SPRITE_SIZE*8,Constant.MARGIN_TOP,Constant.SPRITE_SIZE,Constant.SPRITE_SIZE));
        tankMap.enemyTankList.add(new EnemyTank(Constant.MARGIN_LEFT+Constant.SPRITE_SIZE*12,Constant.MARGIN_TOP,Constant.SPRITE_SIZE,Constant.SPRITE_SIZE));
        tankMap.enemyTankList.add(new EnemyTank(Constant.MARGIN_LEFT+Constant.SPRITE_SIZE*4,Constant.MARGIN_TOP + Constant.SPRITE_SIZE*2,Constant.SPRITE_SIZE,Constant.SPRITE_SIZE));
        tankMap.enemyTankList.add(new EnemyTank(Constant.MARGIN_LEFT+Constant.SPRITE_SIZE*8,Constant.MARGIN_TOP + Constant.SPRITE_SIZE*2,Constant.SPRITE_SIZE,Constant.SPRITE_SIZE));
        //tankMap.boomList.add(new Boom(200 ,200,34,34));//TODO 子弹和墙撞爆炸
    }


/**
 * 游戏的状态1、选择界面 2、游戏运行 3、 4、自定义关卡界面
  */
     public int gameStatus = 1;

    public int getGameStatus() {
        return gameStatus;
    }

    public void setGameStatus(int gameStatus) {
        this.gameStatus = gameStatus;
    }

    /**
     * 重写paint方法
     */
    @Override
    public void paint(Graphics g) {
        if (gameStatus == 1){
            tankSeletor.draw(g);

        }
        else if (gameStatus == 2){

            tankMap.gameRunning(g);
            if(!tankMap.home.isLive() || tankMap.getEnemy_die() >34){
                setGameStatus(5);
                //打死所有坦克的背景音乐
                if (tankMap.getEnemy_die()>34){
                    tankMap.music("src/com/neutech/music/hi.wav");
                }
                //被地方坦克打死的背景音乐
                if (!tankMap.home.isLive()){
                    tankMap.music("src/com/neutech/music/gameOver.wav");
                }
                if (tankMap.getEnemy_die()>=2){
                    g.setColor(Color.white);
            g.setFont(new Font("楷体",Font.BOLD,16));
            g.drawString("20",450,150);
                }
            }
            if (tankMap.p1 == null ){
                setGameStatus(1);
            }

            tankSummary.setIndex(tankMap.getEnemy_die());
//            prop.setIndex(tankMap.getEnemy_die());

        }
        else if (gameStatus == 3){
            tankMap.gameRunning(g);
            if(!tankMap.home.isLive() || tankMap.getEnemy_die() >34){
                setGameStatus(5);
                tankSummary.setIndex(tankMap.getEnemy_die());


            }
            if (tankMap.p1 == null && tankMap.p2 == null){
                setGameStatus(1);
            }
//            tankMap.gameRunning(g);
        }
        else if (gameStatus == 4){
            //这个窗口是利用不同的值调用不同的角色
            mapGenerator.draw(g);
        }
        else if (gameStatus == 5){
            tankSummary.draw(g);


        }
//       g.drawImage(ImageUtils.getValue("tank_main"),0,0,
//               Constant.FRAME_WIDTH,Constant.FRAME_HEIGHT,null);

    }

    Image ImageBuffer = null;
    Graphics GraImage = null;

    public void update(Graphics g){		//覆盖update方法，截取默认的调用过程
        ImageBuffer = createImage(this.getWidth(), this.getHeight());	//创建图形缓冲区
        GraImage = ImageBuffer.getGraphics();		//获取图形缓冲区的图形上下文
        paint(GraImage);		//用paint方法中编写的绘图过程对图形缓冲区绘图
        GraImage.dispose();		//释放图形上下文资源
        g.drawImage(ImageBuffer, 0, 0, this);	//将图形缓冲区绘制到屏幕上
    }


}
